package com.yfp.org.controller;


import cn.hutool.core.bean.BeanUtil;
import com.yfp.common.core.domain.R;
import com.yfp.common.core.utils.poi.ExcelUtil;
import com.yfp.common.core.web.controller.BaseController;
import com.yfp.common.core.web.page.TableDataInfo;
import com.yfp.common.log.annotation.Log;
import com.yfp.common.log.enums.BusinessType;
import com.yfp.org.domain.dto.OrderRefundDTO;
import com.yfp.org.domain.entity.ComboInfo;
import com.yfp.org.domain.entity.ExceptionDerive;
import com.yfp.org.domain.entity.OrderList;
import com.yfp.org.domain.entity.RefundDerive;
import com.yfp.org.domain.vo.OrderListExcelVO;
import com.yfp.org.service.OrderListService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;


/**
 * 订单表(OrderList)表控制层
 *
 *
 * @since 2023-12-28 16:07:36
 */
@RestController
@RequestMapping("orderList")
@Tag(name = "订单接口")
public class OrderListController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private OrderListService orderListService;

    /**
     * 分页查询
     *
     * @param orderList 筛选条件
     * @return 查询结果
     */
    @GetMapping
    @Operation(summary = "分页查询")
    @ApiResponses(value =
            {
                    @ApiResponse(responseCode = "200", description = "Successful response", content = @Content(
                            mediaType = "application/json",
                            schema = @Schema(implementation = OrderList.class)
                    ))
            }
    )
    public TableDataInfo queryByPage(OrderList orderList) {
        return getDataTable(this.orderListService.queryByPage(orderList));
    }


    @GetMapping("/getDeviceOrder")
    @Operation(summary = "设备订单分页查询")
    @ApiResponses(value =
            {
                    @ApiResponse(responseCode = "200", description = "Successful response", content = @Content(
                            mediaType = "application/json",
                            schema = @Schema(implementation = OrderList.class)
                    ))
            }
    )
    public TableDataInfo getDeviceOrder(OrderList orderList) {
        return getDataTable(this.orderListService.getDeviceOrder(orderList));
    }


    @PostMapping("/derive")
    @Operation(summary = "订单列表数据导出", description = "数据导出")
    @Log(title = "订单列表数据导出", businessType = BusinessType.EXPORT)
    public void derive(HttpServletResponse response, @ModelAttribute OrderList orderList) {
        List<OrderList> list = orderListService.derive(orderList);

        if (orderList.getType().equals("Exception")) {
            List<ExceptionDerive> exceptionDerives = BeanUtil.copyToList(list, ExceptionDerive.class);
            ExcelUtil<ExceptionDerive> util = new ExcelUtil<ExceptionDerive>(ExceptionDerive.class);
            util.exportExcel(response, exceptionDerives, "异常订单数据");
        } else if (orderList.getType().equals("refund")) {
            List<RefundDerive> refundDerives = BeanUtil.copyToList(list, RefundDerive.class);
            ExcelUtil<RefundDerive> util = new ExcelUtil<RefundDerive>(RefundDerive.class);
            util.exportExcel(response, refundDerives, "退款取消订单数据");
        } else if (orderList.getType().equals("normal")) {
            List<OrderListExcelVO> excelVOS = BeanUtil.copyToList(list, OrderListExcelVO.class);
            ExcelUtil<OrderListExcelVO> util = new ExcelUtil<OrderListExcelVO>(OrderListExcelVO.class);
            util.exportExcel(response, excelVOS, "订单数据");
        }
    }


    @PostMapping("/exportDevOrderList")
    @Operation(summary = "订单列表数据导出", description = "数据导出")
    @Log(title = "订单列表数据导出", businessType = BusinessType.EXPORT)
    public void exportDevOrderList(HttpServletResponse response, @ModelAttribute OrderList orderList) {
        List<OrderList> list = orderListService.exportDevOrderList(orderList);

        if (orderList.getType().equals("Exception")) {
            List<ExceptionDerive> exceptionDerives = BeanUtil.copyToList(list, ExceptionDerive.class);
            ExcelUtil<ExceptionDerive> util = new ExcelUtil<ExceptionDerive>(ExceptionDerive.class);
            util.exportExcel(response, exceptionDerives, "异常订单数据");
        } else if (orderList.getType().equals("refund")) {
            List<RefundDerive> refundDerives = BeanUtil.copyToList(list, RefundDerive.class);
            ExcelUtil<RefundDerive> util = new ExcelUtil<RefundDerive>(RefundDerive.class);
            util.exportExcel(response, refundDerives, "退款取消订单数据");
        } else if (orderList.getType().equals("normal")) {
            List<OrderListExcelVO> excelVOS = BeanUtil.copyToList(list, OrderListExcelVO.class);
            ExcelUtil<OrderListExcelVO> util = new ExcelUtil<OrderListExcelVO>(OrderListExcelVO.class);
            util.exportExcel(response, excelVOS, "订单数据");
        }
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    @Operation(summary = "通过主键查询单条数据")
    public R<HashMap> queryById(@PathVariable("id") String id) {
        return R.ok(this.orderListService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param orderList 实体
     * @return 新增结果
     */
    @PostMapping
    @Operation(summary = "新增数据")
    public R<OrderList> add(OrderList orderList) {
        return R.ok(this.orderListService.insert(orderList));
    }

    /**
     * 编辑数据
     *
     * @param orderList 实体
     * @return 编辑结果
     */
    @PutMapping
    @Operation(summary = "编辑数据")
    public R<OrderList> edit(OrderList orderList) {
        return this.orderListService.update(orderList);
    }

    /**
     * 标记异常
     *
     * @param
     * @return 编辑结果
     */
    @PutMapping("updateException")
    @Operation(summary = "标记异常")
    public R<OrderList> updateException(@RequestBody OrderList orderList) {
        this.orderListService.updateException(orderList);
        return R.ok();
    }

    @PostMapping("refund")
    @Operation(summary = "退款接口")
    public void refund(@RequestBody OrderRefundDTO orderRefundDTO) {
        this.orderListService.refund(orderRefundDTO);
    }

    @PostMapping("refund/batch")
    @Operation(summary = "退款接口-批量")
    public void refundBatch(@RequestBody List<OrderRefundDTO> orderRefundDTOS) {
        this.orderListService.refundBatch(orderRefundDTOS);
    }

    @GetMapping("tagStatistics")
    @Operation(summary = "标签统计")
    public TableDataInfo tagStatistics(@RequestParam(value = "startTime", required = false) Date startTime,
                                       @RequestParam(value = "endTime", required = false) Date endTime) {
        startPage();

        return getDataTable(this.orderListService.tagStatistics(startTime, endTime));
    }

    @GetMapping("comboStatistics")
    @Operation(summary = "套餐统计")
    public TableDataInfo comboStatistics(@RequestParam(value = "startTime", required = false) Date startTime,
                                         @RequestParam(value = "endTime", required = false) Date endTime,
                                         @RequestParam(value = "cityName", required = false) String cityName) {
        startPage();

        return getDataTable(this.orderListService.comboStatistics(startTime, endTime, cityName));
    }

    @Log(title = "标签统计导出", businessType = BusinessType.EXPORT)
    @Operation(summary = "标签统计导出")
    @PostMapping("/TagDerive")
    public void OrderDerive(HttpServletResponse response,

                            @Parameter(name = "startTime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime,
                            @Parameter(name = "endTime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime) {
        List<com.yfp.org.domain.entity.Tag> comboVOS = this.orderListService.tagStatistics(startTime, endTime);
        ExcelUtil<com.yfp.org.domain.entity.Tag> util = new ExcelUtil<>(com.yfp.org.domain.entity.Tag.class);
        util.exportExcel(response, comboVOS, "标签统计导出");
    }

    @Log(title = "套餐统计导出", businessType = BusinessType.EXPORT)
    @Operation(summary = "套餐统计导出")
    @PostMapping("/ComboDerive")
    public void WorkOrderDerive(HttpServletResponse response,

                                @Parameter(name = "startTime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime,
                                @Parameter(name = "endTime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime,
                                @RequestParam(value = "cityName") String cityName) {
        List<ComboInfo> comboInfoVOS = this.orderListService.comboStatistics(startTime, endTime, cityName);
        ExcelUtil<ComboInfo> util = new ExcelUtil<>(ComboInfo.class);
        util.exportExcel(response, comboInfoVOS, "套餐统计导出");
    }

    @GetMapping("calculate")
    @Operation(summary = "退款金额统计")
    public R calculate(String orderId) {

        return this.orderListService.calculate(orderId);
    }

}
